
#import "BuildaDudeIIAppController.h"
#import "BuildaDudeIIDoc.h"
#import "WW3DWell.h"

@implementation BuildaDudeIIAppController

- init
{
  [super init];
  docList = [[List alloc] init];
  return self;
}

- awake
{
  [super awake];
  docList = [[List alloc] init];
  return self;
}


- newWell:sender
{
   // need to make a new empty well
   currentDoc = [[BuildaDudeIIDoc alloc] init];
   [currentDoc setController:self];
   [docList addObject:currentDoc];
   [[currentDoc window] makeKeyAndOrderFront:self];

    return self;
}

- (BOOL)appAcceptsAnotherFile:sender {  return YES; }

- openFile:(const char *)filename
{
  char         *extension; 


  if (!filename)
  {  return nil;
  }

  extension = rindex((char *)filename, '.');
  if (!extension)
  {  return nil;
  }

  // okay, we should have valid filename now.
  // for whatever it is, we'll need a new well
  [self newWell:self];   

  if (!strcmp(".rib", extension))
  {  [[currentDoc well] buildNewShapeHierarchyFromRIBFile:filename];
  }
  if (!strcmp(".eve", extension))
  {  [[currentDoc well] buildNewShapeHierarchyFromEveFile:filename];
  }
  if (!strcmp(".mdlTemplate", extension) || !strcmp(".wwModelTemplate", extension))
  {  [[currentDoc well] readMdlTemplateFile:filename];
  }
  if (!strcmp(".mdl", extension) || !strcmp(".wwModel", extension))
  {  [[currentDoc well] buildNewShapeHierarchyFromMdlFile:filename];
  }
  if (!strcmp(".nib", extension))
  {  [[currentDoc well] loadNibForModelInterp:filename];
  }

   return self;
}

- open:sender
{
  id           theOpenPanel;
  char         *filterTypes[12] = {"eve", "rib", "mdl", "wwModel", 
                                   "mdlTemplate", "wwModelTemplate", 
                                   "cam", "wwCamera", 
                                   "scn", "wwScene", 
                                   "nib",
                                   NULL };


  theOpenPanel = [OpenPanel new];
  [theOpenPanel allowMultipleFiles:NO];
  if (![theOpenPanel runModalForTypes:filterTypes])
  {  return nil;
  }

  return [self openFile:[theOpenPanel filename]];
}

- close:sender
{
    [[[currentDoc well] window] close];
    return self;
}

- docIsClosing:doc
{
  int   i = 0, 
        howMany = [docList count];
  BOOL  found = NO;


  while (!found & (i < howMany))
  {  if (doc == [docList objectAt:i])
     {  [docList removeObjectAt:i];
        found = YES;
     }
     i++;
  }
  return self;
}


- showInfoPanel:sender
{
   char buf[MAXPATHLEN + 1];
   id bundle;


   if (!infoPanel)
   {  bundle = [NXBundle bundleForClass:[self class]];
      [bundle getPath:buf forResource:"InfoPanel" ofType:"nib"];
      [NXApp loadNibFile:buf owner:self withNames:NO fromZone:[self zone]];
   }
   if (!infoPanel)
   {  NXLogError("problem loading InfoPanel.nib.\n");
      return nil;
   }
   [infoPanel makeKeyAndOrderFront:sender];

   return self;
}


// app delegate stuff...

- appDidInit:sender
{
  [self newWell:self];
  return self;
}

- (int)app:sender openFile:(const char *)filename type:(const char *)aType
{
   if ([self openFile:filename])
   {  return YES;
   }
   return NO;
}

- appWillTerminate:sender
{
   [[docList freeObjects] free];
   return self;
}

@end
